package com.pig4cloud.pig.propertyroom.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.admin.api.dto.UserDTO;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.core.util.RedisUtils;
import com.pig4cloud.pig.propertyroom.dto.PropertyRoomDto;
import com.pig4cloud.pig.propertyroom.entity.PropertyRoom;
import com.pig4cloud.pig.propertyroom.feign.RemoteBuildingService;
import com.pig4cloud.pig.propertyroom.feign.RemoteFloorService;
import com.pig4cloud.pig.propertyroom.feign.RemoteResidentService;
import com.pig4cloud.pig.propertyroom.feign.RemoteUnitService;
import com.pig4cloud.pig.propertyroom.service.RoomService;
import com.pig4cloud.pig.propertyroom.service.RoomVoService;
import com.pig4cloud.pig.propertyroom.vo.PropertyRoomVo;
import com.pig4cloud.pig.resident.feign.dto.ResidentDto;
import com.pig4cloud.pig.resident.feign.dto.UnResidentDto;
import com.pig4cloud.pig.resident.feign.entity.Resident;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/room" )
@Tag(description = "room" , name = "房间管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)

public class RoomController {
	@Autowired
	private final RoomService roomService;
	@Autowired
	private RoomVoService roomVoService;
	@Autowired
	private RemoteUnitService remoteUnitService;
	@Autowired
	private RemoteBuildingService remoteBuildingService;
	@Autowired
	private RemoteFloorService remoteFloorService;
	@Autowired
	private RemoteResidentService remoteResidentService;

	private RedisUtils redisUtils;


	@PostMapping("/addRoom")
	public R addRoom(@RequestBody PropertyRoom roomEntity) {
		PropertyRoom propertyRoom = roomService.getByFloorIdAndRoomNumber(roomEntity.getFloorId(), roomEntity.getRoomNumber());
		if (propertyRoom != null) {
			return R.failed("房间已存在");
		}
		return R.ok(roomService.save(roomEntity));
	}

	@GetMapping("/page")
	public R getUserPage(@ParameterObject Page page, @ParameterObject PropertyRoomDto propertyRoomDto) {
		Page<PropertyRoomVo> result = roomVoService.wrapperPage(page, propertyRoomDto);
		return R.ok(result);
	}

	@GetMapping("/getRoom")
	public R getRoom(@RequestParam Long roomId) {
		PropertyRoomVo room = roomVoService.getByRoomId(roomId);
		return R.ok(room);
	}

	@GetMapping("/getBuilding")
	public R getBuilding(){
		return R.ok(remoteBuildingService.getAll());
	}

	@GetMapping("/getUnit")
	public R getUnit(@RequestParam("buildingId") Long buildingId) {
		return R.ok(remoteUnitService.getUnit(buildingId));
	}

	@GetMapping("/getFloor")
	public R getFloor(@RequestParam("unitId") Long unitId) {
		return R.ok(remoteFloorService.getFloor(unitId));
	}

	@GetMapping("/getResident")
	public R getResident(@RequestParam Long roomId) {
		return R.ok(remoteResidentService.getResident(roomId));
	}

	@GetMapping("/getUnResident")
	public R getUnResident(@ParameterObject Page page, @ParameterObject ResidentDto residentDto) {
		UnResidentDto unResidentDto = new UnResidentDto();
		unResidentDto.setCurrent(page.getCurrent());
		unResidentDto.setSize(page.getSize());
		unResidentDto.setResidentName(residentDto.getResidentName());
		unResidentDto.setPhone(residentDto.getPhone());
		unResidentDto.setRoomId(residentDto.getRoomId());
		return R.ok(remoteResidentService.getUnResident(unResidentDto));
	}

	@DeleteMapping("/deleteResident")
	public R deleteResident(@RequestParam Long[] ids, @RequestParam Long roomId) {
		remoteResidentService.deleteRelativeResident(ids, roomId);
		return R.ok();
	}

	@GetMapping("/addRelativeResident")
	public R addRelativeResident(@RequestParam Long[] ids, @RequestParam Long roomId) {
		remoteResidentService.addRelativeResident(ids, roomId);
		return R.ok();
	}

	@DeleteMapping("/deleteRoom")
	public R deleteRoom(@RequestParam Long[] ids) {
		Resident[] resident = roomService.selectResidentByRoomId(ids);
		if (resident.length > 0) {
			return R.failed("该房间下有住户存在，不能删除");
		}
		List<Long> list = new ArrayList<>();
		Collections.addAll(list, ids);
		roomService.removeByIds(list);
		return R.ok(null, "删除成功");
	}

	@PutMapping("/updateRoom")
	public R updateRoom(@RequestBody PropertyRoom roomEntity) {
		PropertyRoom room = roomService.checkRoomByNumber(roomEntity);
		if (room != null) {
			return R.failed("房间号已存在");
		}
		return R.ok(roomService.updateById(roomEntity));
	}
}
